package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsAttributeMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/22 23:38
 * description:
 */
@Service
public class DtsGoodsAttributeServiceImpl implements DtsGoodsAttributeService {

    @Autowired
    DtsGoodsAttributeMapper attributeMapper;

    @Override
    public void updateAttributes(Integer id, DtsGoodsAttribute[] attributes) {
        //根据商品id删除
        LambdaQueryWrapper<DtsGoodsAttribute> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsAttribute::getGoodsId,id);
        attributeMapper.delete(qw);

        //添加新的库存数据
        if (attributes!=null&&attributes.length>0){
            for (DtsGoodsAttribute attribute : attributes) {
                attribute.setAddTime(new Date());
                attribute.setUpdateTime(new Date());
                attribute.setGoodsId(id);
                attribute.setDeleted(false);
                attributeMapper.insert(attribute);
            }
        }
    }

    @Override
    public void deteleByGoodsId(Integer id) {
        //条件是商品id，修改状态

        DtsGoodsAttribute attribute=new DtsGoodsAttribute();
        attribute.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsAttribute> qw=new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsAttribute::getGoodsId,id);
        attributeMapper.update(attribute,qw);

    }

    @Override
    public void addAttribute(Integer id, DtsGoodsAttribute[] attributes) {
        if (attributes!=null&&attributes.length>0){
            for (DtsGoodsAttribute attribute : attributes) {
                attribute.setAddTime(new Date());
                attribute.setUpdateTime(new Date());
                attribute.setGoodsId(id);
                attribute.setDeleted(false);
                attributeMapper.insert(attribute);
            }
        }
    }
}
